package tech.abing.spider.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.request.PageRequest;

public class JsoupUtil {
	
	private static Logger logger = Logger.getLogger(JsoupUtil.class);
	
	/**
	 * 抽取form表单中所有的键值对
	 * @param form
	 * @param exclude  排除的字段
	 * @return
	 */
	public static Map<String,String> extraFormValue(Element form ,String ... exclude){
		if(form == null){
			return null;
		}
		Map<String,String> keyValuePair = new HashMap<String, String>();
		List<String> eliminate = null;
		if(exclude != null){
			eliminate = Arrays.asList(exclude);
		}
		Elements inputs = form.select("input[name]");
		for (Element input : inputs) {
			String name = input.attr("name");
			if( eliminate!=null && eliminate.contains(name)){
				continue;
			}
			keyValuePair.put(name, input.attr("value"));
		}
		return keyValuePair;
	}
	
	public static String fetchByJsoup(PageRequest request, int timeout) {
		String result = null;
		
		Response response = fetchResponseByJsoup(request, timeout);
		
		if(response != null){
			result = response.body();
		}
		
		return result;
	}
	
	public static Response fetchResponseByJsoup(PageRequest request, int timeout) {
		Response response = null;
		try {
			
			Connection con = Jsoup.connect(request.getUrl()).timeout(timeout * 1000);//获取连接
			
			String cookie = StringUtils.toString(request.getParamsByName("cookie"));
			String referer = StringUtils.toString(request.getParamsByName("referer"));
			
			con.header("Accept-Encoding", "gzip, deflate");   
			con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
			con.header("Accept", "*/*");
			con.header("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			con.header("Accept-Encoding", "gzip, deflate");
			con.header("Connection", "keep-alive");
			con.header("Cookie", cookie);
			con.header("Referer", referer);
			
			response = con.ignoreContentType(true).method(Method.GET).execute();
			
		} catch (Exception e) {
			logger.error("Jsoup请求url["+request.getUrl()+"]失败", e);
		}
		return response;
	}
	
	public static String fetchByJsoup(String url) {
		Document doc = null;
		String result = null;
		try {
			
			Connection con = Jsoup.connect(url).timeout(15 * 1000);//获取连接
			
			con.header("Accept-Encoding", "gzip, deflate");   
			con.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
			con.header("Accept", "*/*");
			con.header("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
			con.header("Accept-Encoding", "gzip, deflate");
			con.header("Connection", "keep-alive");
			
			doc = con.ignoreContentType(true).get();
			result = doc.html();
			
		} catch (Exception e) {
			logger.error("Jsoup请求url["+url+"]失败", e);
		}
		return result;
	}
}
